import django_filters
from django.db.models import Q
from django_filters.rest_framework import OrderingFilter, FilterSet

from goods.models import Goods


class GoodsOrderFilter(OrderingFilter):
    """
    商品展示顺序过滤类
    """

class GoodsFilter(FilterSet):
    """
    商品过滤的类
    """
    top_category = django_filters.NumberFilter(name="category", method='top_category_filter')

    def top_category_filter(self, queryset, value):
        # 不管当前点击的是一级分类二级分类都能找到。
        return queryset.filter(Q(category_id=value) | Q(category__parent_category_id=value))

    class Meta:
        model = Goods
        fields = ['is_new']
